#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100010;

int n, m;
int a[N];

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)   scanf("%d", &a[i]);

    sort(a + 1, a + 1 + n, greater<int>());
    int res = 0;
    for (int i = 1, j = n; i <= n; i++)
    {
        while (j && a[j] < i)    j--;
        if (a[i] >= i - 1 && i - j <= m)
            res = i;
    }
    printf("%d\n", res);
    return 0;
}